﻿using RXY_Mall.Core;
using System;
using System.Web;
using System.Web.Http;
using System.Web.Http.Controllers;
using RXY_Mall.Data;
using RXY_Mall.Domain;
using System.Threading.Tasks;
using System.Collections.Generic;
using RXY_Mall.Core.Tool;

namespace RXY_Mall.AdminApi.Controllers
{
    /// <summary>
    /// 基本类接口
    /// </summary>
    public abstract class BaseApiController : ApiController
    {
        //public int? UserId { get; set; }

        protected override void Initialize(HttpControllerContext controllerContext)
        {
            base.Initialize(controllerContext);
            HttpRequest request = HttpContext.Current.Request;
            
            Task.Run(() => { AddRecordUrl(request); });

            //UserId = (int)SessionHelper.Get("qujia");
        }

        /// <summary>
        /// 记录访问日志
        /// </summary>
        /// <param name="request"></param>
        private void AddRecordUrl(HttpRequest request)
        {
            try
            {
                using (RXY_MallContext db = new RXY_MallContext())
                {
                    gw_security_url model = new gw_security_url()
                    {
                        ip = request.ServerVariables.Get("Remote_Addr").ToString(),
                        url = request.Url.AbsoluteUri,
                        add_time = DateTime.Now,
                        is_check = false,
                    };
                    db.gw_security_urls.Add(model);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex.ToString());
            }
        }
    }




}